﻿using NModBusDrive;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Timers;

namespace TestModBus.Model
{
    public class Form2Model
    {
        public BussinessModel bussiness = new BussinessModel();
        public System.Timers.Timer tmer = null;
        public Form2Model() 
        {
            tmer = new System.Timers.Timer(800);
            tmer.Elapsed += new System.Timers.ElapsedEventHandler(TimerCallBack);
            tmer.AutoReset = true;
            tmer.Enabled = false;
        }

        /// <summary>
        /// 读取状态数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TimerCallBack(object sender, ElapsedEventArgs e)
        {
          
        }

        /// <summary>
        /// 切换通道
        /// </summary>
        /// <param name="value">值</param>
        public void SetRunCh(string value) 
        {
            bussiness.now_function = value;
            bussiness.SendCh();
        }

        /// <summary>
        /// 射频切换操作
        /// </summary>
        /// <param name="operType">操作模式</param>
        public void SetShePinArgs(ModeOperType modeOper) 
        {
            bussiness.operTypeBt = modeOper;
            switch (bussiness.operTypeBt)
            {
                case ModeOperType.Y0:
                    if (bussiness.BtY0 == 0)
                    {
                        bussiness.RegisterBuffer[16] = 1;
                        bussiness.BtY0 = 1;
                    }
                    else
                    {
                        bussiness.BtY0 = 0;
                        bussiness.RegisterBuffer[16] = 0;
                    }
                    break;
                case ModeOperType.Y1:
                    if (bussiness.BtY1 == 0)
                    {
                        bussiness.BtY1 = 1;
                        bussiness.RegisterBuffer[15] = 1;
                    }
                    else
                    {
                        bussiness.BtY1 = 0;
                        bussiness.RegisterBuffer[15] = 0;
                    }

                    break;
                case ModeOperType.Y2:
                    if (bussiness.BtY2 == 0)
                    {
                        bussiness.BtY2 = 1;
                        bussiness.RegisterBuffer[14] = 1;
                    }
                    else
                    {
                        bussiness.BtY2 = 0;
                        bussiness.RegisterBuffer[14] = 0;
                    }
                    break;
                case ModeOperType.Y3:
                    if (bussiness.BtY3 == 0)
                    {
                        bussiness.BtY3 = 1;
                        bussiness.RegisterBuffer[13] = 1;
                    }
                    else
                    {
                        bussiness.BtY3 = 0;
                        bussiness.RegisterBuffer[13] = 0;
                    }
                    break;
                case ModeOperType.Y4:
                    if (bussiness.BtY4 == 0)
                    {
                        bussiness.BtY4 = 1;
                        bussiness.RegisterBuffer[12] = 1;
                    }
                    else
                    {
                        bussiness.BtY4 = 0;
                        bussiness.RegisterBuffer[12] = 0;
                    }
                    break;
                case ModeOperType.Y5:
                    if (bussiness.BtY5 == 0)
                    {
                        bussiness.BtY5 = 1;
                        bussiness.RegisterBuffer[11] = 1;
                    }
                    else
                    {
                        bussiness.BtY5 = 0;
                        bussiness.RegisterBuffer[11] = 0;
                    }
                    break;
                case ModeOperType.Y6:
                    if (bussiness.BtY6 == 0)
                    {
                        bussiness.BtY6 = 1;
                        bussiness.RegisterBuffer[10] = 1;
                    }
                    else
                    {
                        bussiness.BtY6 = 0;
                        bussiness.RegisterBuffer[10] = 0;
                    }
                    break;
                case ModeOperType.Y7:
                    if (bussiness.BtY7 == 0)
                    {
                        bussiness.BtY7 = 1;
                        bussiness.RegisterBuffer[9] = 1;
                    }
                    else
                    {
                        bussiness.BtY7 = 0;
                        bussiness.RegisterBuffer[9] = 0;
                    }
                    break;
                case ModeOperType.Y10:
                    if (bussiness.BtY10 == 0)
                    {
                        bussiness.BtY10 = 1;
                        bussiness.RegisterBuffer[8] = 1;
                    }
                    else
                    {
                        bussiness.BtY10 = 0;
                        bussiness.RegisterBuffer[8] = 0;
                    }
                    break;
                case ModeOperType.Y11:
                    if (bussiness.BtY11 == 0)
                    {
                        bussiness.BtY11 = 1;
                        bussiness.RegisterBuffer[7] = 1;
                    }
                    else
                    {
                        bussiness.BtY11 = 0;
                        bussiness.RegisterBuffer[7] = 0;
                    }
                    break;
                case ModeOperType.Y12:
                    if (bussiness.BtY12 == 0)
                    {
                        bussiness.BtY12 = 1;
                        bussiness.RegisterBuffer[6] = 1;
                    }
                    else
                    {
                        bussiness.BtY12 = 0;
                        bussiness.RegisterBuffer[6] = 0;
                    }
                    break;
                case ModeOperType.Y13:
                    if (bussiness.BtY13 == 0)
                    {
                        bussiness.BtY13 = 1;
                        bussiness.RegisterBuffer[5] = 1;
                    }
                    else
                    {
                        bussiness.BtY13 = 0;
                        bussiness.RegisterBuffer[5] = 0;
                    }
                    break;
                case ModeOperType.Y14:
                    if (bussiness.BtY14 == 0)
                    {
                        bussiness.BtY14 = 1;
                        bussiness.RegisterBuffer[4] = 1;
                    }
                    else
                    {
                        bussiness.BtY14 = 0;
                        bussiness.RegisterBuffer[4] = 0;
                    }
                    break;
                case ModeOperType.Y15:
                    if (bussiness.BtY15 == 0)
                    {
                        bussiness.BtY15 = 1;
                        bussiness.RegisterBuffer[3] = 1;
                    }
                    else
                    {
                        bussiness.BtY15 = 0;
                        bussiness.RegisterBuffer[3] = 0;
                    }
                    break;
                case ModeOperType.Y16:
                    if (bussiness.BtY16 == 0)
                    {
                        bussiness.BtY16 = 1;
                        bussiness.RegisterBuffer[2] = 1;
                    }
                    else
                    {
                        bussiness.BtY16 = 0;
                        bussiness.RegisterBuffer[2] = 0;
                    }
                    break;
                case ModeOperType.Y17:
                    if (bussiness.BtY17 == 0)
                    {
                        bussiness.BtY17 = 1;
                        bussiness.RegisterBuffer[1] = 1;
                    }
                    else
                    {
                        bussiness.BtY17 = 0;
                        bussiness.RegisterBuffer[1] = 0;
                    }
                    break;
                case ModeOperType.Y20:
                    if (bussiness.BtY20 == 0)
                    {
                        bussiness.BtY20 = 1;
                        bussiness.RegisterBuffer[0] = 1;
                    }
                    else
                    {
                        bussiness.BtY20 = 0;
                        bussiness.RegisterBuffer[0] = 0;
                    }
                    break;
            }
        }

        /// <summary>
        /// 执行射频矩阵切换
        /// </summary>
        public void RunMaxShePinChange() 
        {
            bussiness.SendShePin();
        }


        /// <summary>
        /// 颜色定义
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        public Color SetRunColorState(ColorType state)
        {
            Color color = Color.Transparent;
            switch (state)
            {
                case ColorType.Default:
                    color = Color.DarkGray;
                    break;
                case ColorType.Error:
                    color = Color.Red;
                    break;
                case ColorType.Success:
                    color = Color.Green;
                    break;
            }
            return color;
        }
    }
}
